﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.Common
{
    /// <summary>
    /// Summary description for RentCarInput.
    /// </summary>
    public class RentCarInput : System.Web.UI.Page
    {
        #region Variables
        protected System.Web.UI.WebControls.DropDownList Dp_VehicleType;
        protected System.Web.UI.WebControls.TextBox Ctl_NumberOfVehicle;
        protected System.Web.UI.WebControls.Button Btn_Save;
        //protected System.Web.UI.WebControls.ImageButton Btn_Delete;
        protected System.Web.UI.WebControls.Button Btn_Close;
        string FormType = "";
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            string RowIDStr = Request["RowID"];
            string StateStr = Request["State"];
            FormType = Request["FormType"];

            if (RowIDStr != null)
            {
                this.RowID = Convert.ToInt32(RowIDStr);
            }
            else
            {
                this.RowID = 0;
            }
            this.State = StateStr;

            if (!this.IsPostBack)
            {
                this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
                //this.Btn_Delete.Attributes["onclick"] = "javascript:OnDelete();";
                this.Btn_Close.Attributes["onclick"] = "javascript:OnClose();";
                //this.Btn_Save.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_save.gif";
                //this.Btn_Delete.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_delete.gif";
                //this.Btn_Close.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_close.gif";

                WebUtility.BuildVehicleType(this.Dp_VehicleType, BEGDC.CC.BL.Common.DicType.LeaseCar);
                DisplayCarInfo();

                if (this.RowID >= 0 && State == "Edit")
                {
                    //this.Btn_Delete.Visible = true;
                    InitForm();
                }
            }
        }

        #region Display Car Information
        private void DisplayCarInfo()
        {
            if (this.Dp_VehicleType.SelectedItem != null)
            {
                int TypeID = Convert.ToInt32(this.Dp_VehicleType.SelectedValue);
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                BEGDC.CC.Entity.VehicleType vehty = CRService.GetVehicleType(TypeID);
                //this.Lb_Price.Text = vehty.Price.ToString();
            }
        }
        #endregion

        #region RowID State
        protected int RowID
        {
            get { return (int)this.ViewState["LeaseCarRowID"]; }
            set { this.ViewState["LeaseCarRowID"] = value; }
        }
        protected string State
        {
            get { return this.ViewState["LeaseCarState"].ToString(); }
            set { this.ViewState["LeaseCarState"] = value; }
        }
        #endregion

        #region ObtainBookingFromDataTable
        //		private DataRow ObtainBookingFromDataTable(DataTable datatable)
        //		{
        //			if(datatable==null||datatable.Rows.Count==0||this.BookingID==0) return null;
        //
        //			string IdStr = null;
        //			for(int i=0;i<datatable.Rows.Count;i++)
        //			{
        //				IdStr = datatable.Rows[i]["Id"].ToString();
        //				int IdInt = Convert.ToInt32(IdStr);
        //				if(IdInt==this.BookingID)
        //				{
        //					return datatable.Rows[i];
        //				}
        //			}
        //			return null;
        //		}
        #endregion

        #region InitForm
        private void InitForm()
        {
            DataTable CarDataTable = null;
            DataRow CarRow = null;

            object SessLeaseCarBooking = null;
            if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.EZVisitForm)
            {
                SessLeaseCarBooking = Session[SessionFlag.LeaseCarSessionEZ];
            }
            else if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.NormalForm)
            {
                SessLeaseCarBooking = Session[SessionFlag.LeaseCarSession];
            }

            if (SessLeaseCarBooking != null)
            {
                CarDataTable = (DataTable)SessLeaseCarBooking;
            }
            else
            {
                return;
            }

            if (this.RowID >= 0)
            {
                CarRow = CarDataTable.Rows[this.RowID];
                this.Dp_VehicleType.SelectedIndex = Dp_VehicleType.Items.IndexOf(Dp_VehicleType.Items.FindByValue(CarRow["Id"].ToString()));
                this.Ctl_NumberOfVehicle.Text = CarRow["VehicleNumber"].ToString();
            }
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Dp_VehicleType.SelectedIndexChanged += new System.EventHandler(this.VehicleType_SelectChange);
            this.Btn_Save.Click += new EventHandler(Btn_Save_Click);
            //this.Btn_Delete.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Delete_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region Update Booking Session
        private void UpdateBookingSession()
        {
            DataTable dataTable = null;
            DataRow dataRow = null;
            //object SessLeaseCarBooking = Session[SessionFlag.LeaseCarSession];
            object SessLeaseCarBooking = null;
            if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.EZVisitForm)
            {
                SessLeaseCarBooking = Session[SessionFlag.LeaseCarSessionEZ];
            }
            else if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.NormalForm)
            {
                SessLeaseCarBooking = Session[SessionFlag.LeaseCarSession];
            }

            if (SessLeaseCarBooking != null)
            {
                dataTable = (DataTable)SessLeaseCarBooking;
            }
            else
            {
                return;
            }

            if (this.State == "New")
            {
                dataRow = dataTable.NewRow();

                dataRow["Id"] = "0";
                dataRow["VehicleTypeId"] = this.Dp_VehicleType.SelectedItem.Value.ToString();
                dataRow["VehicleType"] = this.Dp_VehicleType.SelectedItem.Text;
                dataRow["VehicleNumber"] = this.Ctl_NumberOfVehicle.Text;

                dataTable.Rows.Add(dataRow);
            }
            else if (this.State == "Edit" && this.RowID >= 0)
            {
                dataRow = dataTable.Rows[this.RowID];

                dataRow["VehicleTypeId"] = this.Dp_VehicleType.SelectedItem.Value.ToString();
                dataRow["VehicleType"] = this.Dp_VehicleType.SelectedItem.Text;
                dataRow["VehicleNumber"] = this.Ctl_NumberOfVehicle.Text;
            }
            else if (this.State == "Delete" && this.RowID >= 0)
            {
                dataTable.Rows.RemoveAt(this.RowID);
            }
            else
            {
                return;
            }

            //Session[SessionFlag.LeaseCarSession] = dataTable;
            if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.EZVisitForm)
            {
                Session[SessionFlag.LeaseCarSessionEZ] = dataTable;
            }
            else if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.NormalForm)
            {
                Session[SessionFlag.LeaseCarSession] = dataTable;
            }
        }
        #endregion

        #region Save
        void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateBookingSession();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }
        #endregion

        #region Delete
        private void Btn_Delete_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            try
            {
                this.State = "Delete";
                UpdateBookingSession();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }
        #endregion

        #region Select Index change
        private void VehicleType_SelectChange(object sender, System.EventArgs e)
        {
            DisplayCarInfo();
        }
        #endregion
    }
}
